<?php
/**
 * WeChatService.php
 * 文件描述 微信相关服务类
 * Created on 2020/9/4 9:15
 * Created by 刘小龙 595929049@qq.com
 */

namespace app\common\services;

use EasyWeChat\Factory;

class WeChatService
{
    public $payment;

    public function __construct()
    {
        $this->payment = Factory::payment(config('wechat.payment'));
    }

    /**
     * @todo 微信支付：企业付款到用户
     * @param string $open_id       用户openID
     * @param string $trade_no      提现订单号
     * @param int $amount           提现金额，单位分
     * @return array
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     *
        // 参数错误
        [
            "return_code" => "SUCCESS"
            "return_msg" => "参数错误：校验用户姓名选项check_name字段必填."
            "result_code" => "FAIL"
            "err_code" => "PARAM_ERROR"
            "err_code_des" => "参数错误：校验用户姓名选项check_name字段必填."
        ]
        // 签名错误
        [
            "return_code" => "SUCCESS"
            "return_msg" => "SIGN_ERROR"
            "mch_appid" => "wxbbcf11c4550b998c"
            "mchid" => "1602404993"
            "result_code" => "FAIL"
            "err_code" => "SIGN_ERROR"
            "err_code_des" => "签名错误"
        ]
        // 成功提现
        [
            "return_code" => "SUCCESS"
            "return_msg" => null
            "mch_appid" => "wxbbcf11c4550b998c"
            "mchid" => "1602404993"
            "nonce_str" => "5f58a46b089cc"
            "result_code" => "SUCCESS"
            "partner_trade_no" => "TX1599644779023941"
            "payment_no" => "10101234233712009096481519266394"
            "payment_time" => "2020-09-09 17:45:55"
        ]
     */
    public function payToBalance(string $open_id, string $trade_no, int $amount)
    {
        return $this->payment->transfer->toBalance([
            'check_name'        => 'NO_CHECK',
            'openid'            => $open_id,
            'partner_trade_no'  => $trade_no,
            'amount'            => $amount,
            'desc'              => '恋爱时光提现成功'
        ]);
    }
}
